layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 回传单表管理
     */
    var TransportReceipt = {
        tableId: "transportReceiptTable"
    };

    /**
     * 初始化表格的列
     */
    TransportReceipt.initColumn = function () {
        return [[
            {field: 'id', hide: true, title: '主键id'},
            {align: 'center', toolbar: '#tableBar', title: '操作',width: 120},
            {field: 'stockPort', title: '<span style="color: #2b85e4;"><i class="layui-icon">&#xe716;</i> 备货口</span>', style:'color: #2b85e4;', edit:'text'},
            {field: 'deliverys', title: '<span style="color: #2b85e4;"><i class="layui-icon">&#xe716;</i> 配送顺序</span>', style:'color: #2b85e4;', edit:'text'},

            {field: 'getUnit', title: '卸货单位', style:'background-color: #dbf1e1;font-weight: 600;', width: 130},
            {field: 'getLocation', title: '卸货地址', style:'background-color: #dbf1e1;font-weight: 600;', width: 170},
            {field: 'longitude', title: '经度', style:'font-weight: 600;'},
            {field: 'latitude', title: '纬度', style:'font-weight: 600;'},
            {field: 'kilometer', title: '公里数', style:'font-weight: 600;'},

            {field: 'num', sort: true, title: '总数量'},
            {field: 'volume', title: '总体积'},
        ]];
    };

    // 编辑单元格 事件
    table.on('edit(' + TransportReceipt.tableId + ')', function (obj) {
        var data = obj.data;//得到所在行所有键值
		var value = obj.value;//修改后的值
        var field = obj.field;
		if(field == 'stockPort'){
	        var ajax = new $ax(Feng.ctxPath + "/transportReceipt/editStockPort?id="+data.id+"&stockPort="+value, function (data) {
	    		if (data.success) {
	                Feng.success("修改成功!");
				} else {
	                Feng.error("修改失败！"+data.message);
				}
	        }, function (data) {
	            Feng.error("修改失败!" + data.responseJSON.message + "!");
	        });
	        ajax.start();
		}
		if(field == 'deliverys'){
	        var ajax = new $ax(Feng.ctxPath + "/transportReceipt/editStockPort?id="+data.id+"&deliverys="+value, function (data) {
	    		if (data.success) {
	                Feng.success("修改成功!");
				} else {
	                Feng.error("修改失败！"+data.message);
				}
	        }, function (data) {
	            Feng.error("修改失败!" + data.responseJSON.message + "!");
	        });
	        ajax.start();
		}
    });

    /**
     * 点击查询按钮
     */
    TransportReceipt.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(TransportReceipt.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TransportReceipt.tableId,
        url: Feng.ctxPath + '/transportReceipt/list_transportSet?dispatchNumber='+Feng.getUrlParam("dispatchNumber"),
        page: true,
        limit: 40,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportReceipt.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportReceipt.search();
    });

    // 工具条点击事件
    table.on('tool(' + TransportReceipt.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (obj.event === 'gaidizhi') {
            layer.open({
                type: 2,
                title: '修改地址',
                area: ['900px', '550px'],
                content: Feng.ctxPath + '/orderTransportDispatch/toGaidizhi?id=' + data.getLocationid + "&setLocationId=" + data.setLocationId,
                end: function () {
                	table.reload(TransportReceipt.tableId);
                }
            });
    	}
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });
});
